﻿using BLL.Manager;
using Helper;
using HZY.Framework.DependencyInjection;
using IoTClient.Common.Enums;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Model.EntityInfo;
using Sunny.UI;
using WinFormsSprayProcessSCADASystem.Extension;
using Yale.SprayProcessSCADASystem;

namespace WinFormsSprayProcessSCADASystem
{

    public partial class FormLogin : UIForm, IScopedSelfDependency
    {
        private readonly ILogger<MainForm> _logger;
        private readonly UserManager _userManager;
        public FormLogin(ILogger<MainForm> _logger, UserManager userManager)
        {
            InitializeComponent();
            this._logger = _logger;
            //初始化配置
            InitConfig();
            this._userManager = userManager;
        }


        #region 初始化配置
        private void InitConfig()
        {
            Globals.PlcVarConfigPath = Globals.IniFile.ReadString("PLC", "path", $"{Application.StartupPath}\\..\\config\\PlcVarConfig.xlsx");
            //CPU类型
            Globals.CpuType = Enum.Parse<SiemensVersion>(Globals.IniFile.ReadString("PLC", "siemensVersion", "S7_1200"));

            //ip地址
            Globals.IpAddress = Globals.IniFile.ReadString("PLC", "ipAddress", "127.0.0.1");

            //端口号
            Globals.Port = Globals.IniFile.ReadInt("PLC", "port", 102);

            //插槽
            Globals.Slot = Globals.IniFile.ReadByte("PLC", "slot", 0);

            //机架号
            Globals.Rack = Globals.IniFile.ReadByte("PLC", "rack", 0);

            //plc连接超时时间
            Globals.ConnectTimeOut = Globals.IniFile.ReadInt("PLC", "connectTimeOut", 2000);

            //plc读取间隔时间
            Globals.ReadTimeInnterval = Globals.IniFile.ReadInt("PLC", "readTimeInnterval", 2000);

            //plc重连间隔时间
            Globals.ReConnectTimeInnterval = Globals.IniFile.ReadInt("PLC", "reConnectTimeInnterval", 2000);
        }
        #endregion

        private async void bt_login_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.tb_userName.Text))
            {

                this.ShowWarning("用户名不能为空");
                return;
            }
            if (string.IsNullOrEmpty(this.tb_password.Text))
            {

                this.ShowWarning("密码不能为空");
                return;
            }
            var loginDto = new BLL.DTO.UserDto.UserLoginDto
            {
                UserName = tb_userName.Text,
                UserPassword = tb_password.Text
            };
            try
            {
                var res = await _userManager.LoginReqAsync(loginDto);
                if (res.Code == SystemEnums.ResCode.Success)
                {
                    this.Hide();
                    Globals.CurrentUser = res.Data;
                    var res2 = await _userManager.GetUserRoutesAsync(new BLL.DTO.UserDto.UserGetMyRoutesDto()
                    {
                        RoleId = res.Data.RoleInfo.Id
                    });
                    Globals.RouteList = res2.Data;
                    Splasher.Show(typeof(FormStartLoad));
                    MainForm mainForm = Globals.ServiceProvider.GetRequiredService<MainForm>();
                    mainForm.Show();
                    this.ActionLogMes("登录了！");
                }
                else
                {
                    this.ShowError(res.Msg);
                }
            }
            catch (Exception ex)
            {

                Console.WriteLine(ex.Message);
            }
        }

        public async void bt_tourist_Click(object sender, EventArgs e)
        {
            var loginDto = new BLL.DTO.UserDto.UserLoginDto
            {
                UserName = "youke",
                UserPassword = "youke"
            };
            try
            {
                var res = await _userManager.LoginReqAsync(loginDto);
                if (res.Code == SystemEnums.ResCode.Success)
                {
                    this.Hide();
                    Globals.CurrentUser = (UserInfoModel)res.Data;
                    var res2 = await _userManager.GetUserRoutesAsync(new BLL.DTO.UserDto.UserGetMyRoutesDto()
                    {
                        RoleId = res.Data.RoleInfo.Id
                    });
                    Globals.RouteList = res2.Data;
                    Splasher.Show(typeof(FormStartLoad));
                    MainForm mainForm = Globals.ServiceProvider.GetRequiredService<MainForm>();
                    mainForm.Show();
                }
                else
                {
                    this.ShowError(res.Msg);
                }
            }
            catch (Exception ex)
            {

                Console.WriteLine(ex.Message);
            }
        }


        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x02000000;
                return cp;
            }
        }
        private void uiSymbolLabel1_Click(object sender, EventArgs e)
        {
            Application.Exit();
            Environment.Exit(0);
        }

    }


}
